package com.hspedu.smallchange;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

/**
 * 该类是完成零钱通的各个功能的类
 */
public class SmallChangeSysOOP {

    //定义属性
    boolean loop = true;
    Scanner scanner = new Scanner(System.in);
    String key = "";

    //1.完成零钱通明细
    //使用 String 拼接
    String details = "--------------零钱通明细--------------";

    //2.完成收益入账
    double money = 0;
    double balance = 0;
    Date date = null;// date 是 java.util.Date 类型，表示日期
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");//可以用于日期格式化

    //3.完成消费
    String name = "";

    //显示主菜单
    public void menu() {
        do {
            System.out.println("\n==============零钱通菜单==============");
            System.out.println("\t\t\t1.零钱通明细");
            System.out.println("\t\t\t2.收益入账");
            System.out.println("\t\t\t3.消     费");
            System.out.println("\t\t\t4.退     出");
            System.out.print("请选择(1-4):");
            key = scanner.next();

            //使用 switch 分支控制
            switch (key) {
                case "1" :
                    this.detailacc();
                    break;
                case "2" :
                    this.income();
                    break;
                case "3" :
                    this.pay();
                    break;
                case "4" :
                    this.exit();
                    break;
                default:
                    System.out.println("选择有误请重新选择");
            }

        } while (loop);

        System.out.println("==============退出了零钱通==============");
    }

    //明细
    public void detailacc() {
        System.out.println(details);
    }

    //收益入账
    public void income() {
        System.out.print("收益入账金额：");
        money = scanner.nextDouble();

        //校验 money 范围
        if (money <= 0) {
            System.out.println("输入金额有误，请重新输入");
            return;//退出方法，不再执行后面的代码
        }

        balance += money;
        //拼接收益入账到details
        date = new Date();//获取当前日期
        details += "\n收益入账\t+" + money + "\t" + sdf.format(date) + "\t余额：" + balance;
    }

    //消费
    public void pay() {
        System.out.print("消费店名：");
        name = scanner.next();
        System.out.print("消费金额：");
        money = scanner.nextDouble();

        //校验 money 范围
        if (money > balance || money <= 0) {
            System.out.println("您的消费金额应该在(0," + balance + "]");
            return;
        }

        balance -= money;
        //拼接消费到details
        date = new Date();//获取当前日期
        details += "\n" + name + "\t-" + money + "\t" + sdf.format(date) + "\t余额：" + balance;
    }

    //退出
    public void exit() {
        String choice = "";
        while (true) {
            System.out.print("你确定要退出吗?y/n:");
            choice = scanner.next();
            if ("y".equals(choice) || "n".equals(choice)) {
                break;
            }
        }

        //当用户输入正确时，退出 while，进行判断
        if (choice.equals("y")) {
            loop = false;
        }
    }

}
